#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "labelparser.h"
#include "formopen.h"
#include "formviewer.h"
#include "dialogpreferences.h"
#include <QTextStream>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui_open = new FormOpen(this);
    ui_pref = new DialogPreferences(this);
    ui_viewer = new FormViewer();
    ui->gridLayoutCentral->addWidget(ui_viewer);
    ui_open->hide();
    ui_pref->hide();
    ui_open->setModal(true);
    style = QApplication::style();

    ui->actionSave_Smil->setIcon(style->standardIcon(QStyle::SP_DialogSaveButton));
    ui->actionOpen->setIcon(style->standardIcon(QStyle::SP_DialogOpenButton));
    ui->mainToolBar->addAction(ui->actionOpen);
    ui->mainToolBar->addAction(ui->actionSave_Smil);
    connect(ui->actionOpen,SIGNAL(triggered()),SLOT(openTags()));
    connect(ui->actionSave_Smil,SIGNAL(triggered()),SLOT(saveXml()));
    connect(ui_open,SIGNAL(accepted()),SLOT(viewPreview()));
    connect(ui->actionPreferences,SIGNAL(triggered()),SLOT(viewPreferences()));
    connect(ui_pref,SIGNAL(accepted()),SLOT(changeColors()));
    p = LabelParser::Instance();

}

MainWindow::~MainWindow()
{
    delete ui;
    delete ui_open;
    delete ui_viewer;
    delete ui_pref;
    delete p;
}

void MainWindow::parse()
{
    QString fileName = ui_open->getLabel();
    QString audioSrc = ui_open->getAudioSrc();
    QString xhtmlSrc = ui_open->getXhtml();

    if(fileName.isEmpty()){
        //QMessageBox::warning(this,"Error","File name is empty!");
        return;
    }
    QFile tag(fileName);
    if(!tag.open(QFile::ReadOnly)){
        QMessageBox::warning(this,"Error","File does not exist!");
        return;
    }
    if(!(p->audioSrc == audioSrc && p->seqRef == xhtmlSrc &&  p->fileName == fileName)){

        p->fileName = fileName;
        p->audioSrc = audioSrc;
        p->seqRef = xhtmlSrc;

        QTextStream stream(&tag);
        QByteArray xml= p->parse(stream);
        if(xml.size() == 0){
            QMessageBox::warning(this,"Error",fileName+" is Not a valid file!");
            return;
        }
        ui_viewer->setText(QString(xml));
    }
    tag.close();

}

void MainWindow::openTags()
{
    ui_open->show();

}

void MainWindow::viewPreview()
{
   parse();
}



void MainWindow::saveXml()
{
    QString xml = ui_viewer->text();
    if(xml.isEmpty()){
        return;
    }
    QString fileName = QFileDialog::getSaveFileName(this,"Save Smil File",QString(),"smil");
    if(!fileName.contains(".smil")){
        fileName += ".smil";
    }
    QFile smil(fileName);

    if(!smil.open(QFile::WriteOnly)){
        QMessageBox::warning(this,"Error","Cannot Save File!");
        return;
    }

    smil.write(xml.toUtf8());
    smil.close();
}

void MainWindow::viewPreferences()
{
  ui_pref->setColors(ui_viewer->Highlighter->getColors());
  ui_pref->show();
}

void MainWindow::changeColors()
{
    qDebug()<<"change Colors";
    ui_viewer->Highlighter->setColors(ui_pref->getColors());
}
